#include "libraries.h"
#include "extern.h"
#include "wallgen.h"

int WallGen(int endX, int endY, int density)
{
	int Roomsamount = (endX-1)*(endY-1)/(density*4);
	bool doors[200][200];
	for (int j = 0; j < 200; j++)
	{
		for (int i = 0; i < 200; i++)
		{
			doors[j][i] = 1;
		}
	}
	for (int x = 0; x < (endX-1)/2; x++)
	{
		for (int y = 0; y < (endY-1)/2; y++)
		{
			if (x > 0) {if (RoomMap[x-1][y] != RoomMap[x][y]) {doors[RoomMap[x][y]][RoomMap[x-1][y]] = 0;} }
			if (x < (endX-1)/2) {if (RoomMap[x+1][y] != RoomMap[x][y]) {doors[RoomMap[x][y]][RoomMap[x+1][y]] = 0;} }
			if (y > 0) {if (RoomMap[x][y-1] != RoomMap[x][y]) {doors[RoomMap[x][y]][RoomMap[x][y-1]] = 0;} }
			if (y < (endY-1)/2) {if (RoomMap[x][y+1] != RoomMap[x][y]) {doors[RoomMap[x][y]][RoomMap[x][y+1]] = 0;} }
		}
	}
	for (int i = 0; i < endX; i++) {Map[i][0] = 0;}
	for (int i = 0; i < endY; i++) {Map[0][i] = 0;}
	for (int x = 0; x < (endX-1)/2; x++)
	{
		for (int y = 0; y < (endY-1)/2; y++)
		{
			Map[x*2+1][y*2+1] = RoomMap[x][y] % 2 + 2;
			Map[x*2+1][y*2+2] = RoomMap[x][y] % 2 + 2;
			Map[x*2+2][y*2+1] = RoomMap[x][y] % 2 + 2;
			Map[x*2+2][y*2+2] = RoomMap[x][y] % 2 + 2;
		}
	}
	for (int x = 0; x < (endX-1)/2; x++)
	{
		for (int y = 0; y < (endY-1)/2; y++)
		{
			if (x < (endX-1)/2) {if (RoomMap[x+1][y] != RoomMap[x][y]) {Map[x*2+2][y*2+1] = 0; Map[x*2+2][y*2+2] = 0;} }
			if (x == (endX-1)/2) {Map[x*2+2][y*2+1] = 0; Map[x*2+2][y*2+2] = 0;}
			if (y < (endY-1)/2) {if (RoomMap[x][y+1] != RoomMap[x][y]) {Map[x*2+1][y*2+2] = 0; Map[x*2+2][y*2+2] = 0;} }
			if (x == (endX-1)/2) {Map[x*2+1][y*2+2] = 0; Map[x*2+2][y*2+2] = 0;}
			if (x < (endX-1)/2 && y < (endY-1)/2) {if (RoomMap[x+1][y+1] != RoomMap[x][y]) {Map[x*2+2][y*2+2] = 0;} }
		}
	}
	for (int i = 0; i < 200; i++)
	{
		doors[0][i] = 1;
		doors[i][0] = 1;
	}
	int Loop = 1;
	while (Loop == 1)
	{
		for (int x = 0; x < (endX-1)/2; x++)
		{
			for (int y = 0; y < (endY-1)/2; y++)
			{
				if (x < (endX-1)/2) {if (doors[RoomMap[x][y]][RoomMap[x+1][y]] == 0) {if (rand() % 2 == 0) {Map[x*2+2][y*2+1] = 1; doors[RoomMap[x][y]][RoomMap[x+1][y]] = 1; doors[RoomMap[x+1][y]][RoomMap[x][y]] = 1;} } }
				if (y < (endY-1)/2) {if (doors[RoomMap[x][y]][RoomMap[x][y+1]] == 0) {if (rand() % 2 == 0) {Map[x*2+1][y*2+2] = 1; doors[RoomMap[x][y]][RoomMap[x][y+1]] = 1; doors[RoomMap[x][y+1]][RoomMap[x][y]] = 1;} } }
			}
		}
		Loop = 0;
		for (int j = 0; j < 200; j++)
		{
			for (int i = 0; i < 200; i++)
			{
				if (doors[j][i] != 1) {j = 200; i = 200; Loop = 1;}
			}
		}
	}
	BDAmount = 0;
	for (int i = 0; i <= endY; i++)
	{
		if (Map[1][i] >= 2)			{if (rand() % 15 == 0)	{Map[0][i] = 1; BorderDoors[BDAmount].X = 0; BorderDoors[BDAmount].Y = i; BDAmount++;}}
		if (Map[endX-2][i] >= 2)	{if (rand() % 15 == 0)	{Map[endX-1][i] = 1; BorderDoors[BDAmount].X = endX-1; BorderDoors[BDAmount].Y = i; BDAmount++;}}
	}
	for (int i = 0; i <= endX; i++)
	{
		if (Map[i][1] >= 2)			{if (rand() % 15 == 0)	{Map[i][0] = 1; BorderDoors[BDAmount].X = i; BorderDoors[BDAmount].Y = 0; BDAmount++;}}
		if (Map[i][endY-2] >= 2)	{if (rand() % 15 == 0)	{Map[i][endY-1] = 1; BorderDoors[BDAmount].X = i; BorderDoors[BDAmount].Y = endY-1; BDAmount++;}}
	}
	return 1;
}